package com.xuan.modules.system.service.impl;
import cn.hutool.crypto.digest.DigestUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuan.commons.utils.Sha256;
import com.xuan.modules.system.entity.User;
import com.xuan.modules.system.mapper.UserMapper;
import com.xuan.modules.system.service.UserService;
import com.xuan.modules.system.utils.SaltUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.crypto.hash.Sha1Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import sun.security.provider.SHA;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 七月初七
 * @since 2021-05-21
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public void register(User user) {
        //获取到随机盐
        String salt = SaltUtils.getSalt(6);
        user.setSalt(salt);
        //进行shiro自带的md5加密
//        Md5Hash md5Hash = new Md5Hash(user.getPassword(),salt,1024);
//        //将生成的密文赋值给用户密码
//        user.setPassword(md5Hash.toHex());
        String pwdAndSalt=user.getPassword()+user.getSalt();//将密码和盐值作为新密码进行加密
        String newPassword = Sha256.encodePassword(pwdAndSalt);
        user.setPassword(newPassword);
        userMapper.insert(user);
    }
}
